/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun.gui;

import com.norbl.util.*;
import javax.swing.*;
import java.io.*;

/**
 *
 * @author Barnet Wagman
 */
public class MonitorParamHandler extends ParamFileHandler {

    public static final String FILE_NAME = ".runjrun_params.txt";

    enum Param {
        awsAccessID, awsSecretKey, bucketName
    }

    public String awsAccessID;
    public String awsSecretKey;
    public String bucketName;

    public MonitorParamHandler() { super(); }

    public boolean getParams(JFrame parent) {

        PropertyList pl = read(FILE_NAME);
        if ( pl != null ) {
            if ( !hasRequiredParams(pl) )
                throw new RuntimeException("The param file does not " +
                            "have all required params.");
            awsAccessID =
                (String) pl.getPropertyValue(Param.awsAccessID.toString());
            awsSecretKey =
                (String) pl.getPropertyValue(Param.awsSecretKey.toString());
            bucketName =
                (String) pl.getPropertyValue(Param.bucketName.toString());
            return(true);
        }
        else return(getNewParams(parent));
    }

    public boolean getParams(JFrame parent, File paramFile) {

        PropertyList pl = read(paramFile);
        if ( pl != null ) {
            if ( !hasRequiredParams(pl) )
                throw new RuntimeException("The param file does not " +
                            "have all required params.");
            awsAccessID =
                (String) pl.getPropertyValue(Param.awsAccessID.toString());
            awsSecretKey =
                (String) pl.getPropertyValue(Param.awsSecretKey.toString());
            bucketName =
                (String) pl.getPropertyValue(Param.bucketName.toString());
            return(true);
        }
        else return(false);
    }

    public boolean getNewParams(JFrame parent) {
        File paramFile = new File(getParamDir(FILE_NAME),FILE_NAME);
        ParamDialog d = new ParamDialog(parent, true, paramFile,
                                        awsAccessID,
                                        awsSecretKey,
                                        bucketName);
        d.run();
        if ( d.paramsChanged ) {
            awsAccessID = d.awsAccessID;
            awsSecretKey = d.awsSecretKey;
            bucketName = d.bucketName;
            if ( d.saveParams ) {
                PropertyList npl = new PropertyList();
                npl.add(Param.awsAccessID.toString(),awsAccessID);
                npl.add(Param.awsSecretKey.toString(),awsSecretKey);
                npl.add(Param.bucketName.toString(),bucketName);
                write(d.paramFile,npl);
            }
            return(true);
        }
        return(false); // User cancelled.
    }

    public boolean hasRequiredParams(PropertyList pl) {
        return( pl.hasProperty(Param.awsAccessID.toString()) &&
                pl.hasProperty(Param.awsSecretKey.toString()) &&
                pl.hasProperty(Param.bucketName.toString()) );
    }
}
